package com.study.javaBasic.thread_pool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ExecutorsTest {

    private static final Integer THREAD_COUNT=12;

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ThreadPoolExecutor executor = getExecutor();
        List<Future<String>> result=new ArrayList<>(THREAD_COUNT);
        for (int i=0; i<THREAD_COUNT;i++){
            result.add(executor.submit(new MyCallable("thread-" + i)));
        }
        System.out.println("    executor.getActiveCount() = " + executor.getActiveCount());

        for (Future<String> stringFuture : result) {
            System.out.println("stringFuture.get() = " + stringFuture.get(4,TimeUnit.SECONDS));
            System.out.println("    executor.getActiveCount() = " + executor.getActiveCount());
        }
        System.out.println("    executor.getActiveCount() = " + executor.getActiveCount());
    }

    public static ThreadPoolExecutor getExecutor(){
        return new ThreadPoolExecutor(1,10,10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(1));
    }

    static class MyCallable implements Callable<String>, Delayed{

        private String name;

        public MyCallable(String name){
            this.name=name;
        }
        @Override
        public String call() throws Exception {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return name+":"+"hello";
        }

        @Override
        public long getDelay(TimeUnit unit) {
            return 0;
        }

        @Override
        public int compareTo(Delayed o) {
            return 0;
        }
    }
}
